#include <linux/module.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/ethtool.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/ip.h> 

static struct net_device    *virt_net;

static void virt_rs_packet(struct sk_buff *skb, struct net_device *dev)
{
       unsigned char *type;
       struct iphdr *ih;
       __be32 *saddr, *daddr, tmp;
       unsigned char tmp_dev_addr[ETH_ALEN];
       struct ethhdr *ethhdr;
       struct sk_buff *rx_skb;

       ethhdr = (struct ethhdr *)skb->data;
       memcpy(tmp_dev_addr, ethhdr->h_dest, ETH_ALEN);
       memcpy(ethhdr->h_dest, ethhdr->h_source, ETH_ALEN);
       memcpy(ethhdr->h_source, tmp_dev_addr, ETH_ALEN);

       ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
       saddr = &ih->saddr;
       daddr = &ih->daddr;
       tmp = *saddr;
       *saddr = *daddr;
       *daddr = tmp;

        ih->check = 0;               
        ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);

       type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);
       *type = 0;
       rx_skb = dev_alloc_skb(skb->len + 2);
   
       skb_reserve(rx_skb, 2);

    memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len);

    rx_skb->dev = dev;
    rx_skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */

    rx_skb->protocol = eth_type_trans(rx_skb, dev);

       dev->stats.rx_packets++;                     
       dev->stats.rx_bytes += skb->len;
//       dev->last_rx= jiffies;

       netif_rx(rx_skb);

}

static int virt_send_packet(struct sk_buff *skb, struct net_device *dev)
{
// add it into net_ops->open
//netif_stop_queue(dev);
 
    virt_rs_packet(skb,dev);
 
     dev_kfree_skb(skb);

    dev->stats.tx_packets++;
    dev->stats.tx_bytes+=skb->len;
    netdev_get_tx_queue(dev,0)->trans_start = jiffies;

    netif_wake_queue(dev); 

    return 0;
}

static int vnet_open(struct net_device *dev)
{
        netif_start_queue(dev);
        return 0;
}

static int vnet_stop(struct net_device *dev)
{
        netif_stop_queue(dev);
        return 0;
}



static const struct net_device_ops vnet_ops = {
	
	.ndo_open	= vnet_open,
	.ndo_stop	= vnet_stop,
	.ndo_start_xmit = virt_send_packet,
};
 
static u32 always_on(struct net_device*dev)
{
	return 1;
}

static const struct ethtool_ops l_ethtool_ops = {
        .get_link               = always_on,
        .get_ts_info            = ethtool_op_get_ts_info,
};

static int __init virt_net_init(void)
{
    virt_net= alloc_netdev(sizeof(struct net_device), "virt_eth0",NET_NAME_UNKNOWN, ether_setup);

    virt_net->netdev_ops = &vnet_ops;

    virt_net->ethtool_ops = &l_ethtool_ops;

#if 0
    virt_net->dev_addr[0] = 0x08;　　　　 
    virt_net->dev_addr[1] = 0x89;
    virt_net->dev_addr[2] = 0x89;
    virt_net->dev_addr[3] = 0x89;
    virt_net->dev_addr[4] = 0x89;
    virt_net->dev_addr[5] = 0x89;

    virt_net->features        |= NETIF_F_NO_CSUM;
#else
    // using random MAC addr
     eth_hw_addr_random(virt_net);
#endif
    virt_net->features        |= NETIF_F_LOOPBACK_BIT;

    virt_net->flags           |= IFF_NOARP;
    
    virt_net->mtu = 1450;

    register_netdev(virt_net);
    return 0;
}

static void __exit virt_net_exit(void)
{
    unregister_netdev(virt_net);
    free_netdev(virt_net);   
}

module_init(virt_net_init);
module_exit(virt_net_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("by:zhang");

